#include<iostream>
#include<vector>
using namespace std;

int maxSubarraySumCircular(vector<int>& nums) {
    int n = nums.size();
    vector<int> f(n + 1), g(n + 1);
    int fmax = -0x3f3f3f, gmin = 0x3f3f3f, sum = 0;

    for (int i = 1; i <= n; i++) {
        int x = nums[i - 1];
        f[i] = max(x, x + f[i - 1]);
        fmax = max(fmax, f[i]);
        g[i] = min(x, x + g[i - 1]);
        gmin = min(gmin, g[i]);
        sum += x;
    }

    return sum == gmin ? fmax : max(fmax, sum - gmin);
}


int main()
{
    vector<int>nums = { 5,-1,5 };
    cout << maxSubarraySumCircular(nums) << endl;
    return 0;
}